package club.smartdot.transcation;

import java.util.List;

import club.smartdot.util.RedisUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Transaction;

public class RedisTransaction {
	public static void main(String[] args) {
		// 创建Jedis对象，连接到Redis, 需要提供ip和port
		JedisPool pool = RedisUtil.open("127.0.0.1", 6379);
		Jedis jedis = null;
		
		try {
			jedis = pool.getResource();
			//开启事物
			Transaction transaction = jedis.multi();
			// 添加字符串
			transaction.mset("breakfaset", "豆浆和包子");
			// 一次设置多个key-value
			transaction.mset("lunch", "红烧肉", "dinner", "牛肉面");
			List<Object> results = transaction.exec();
			// 事物的处理结果
			for (Object result : results) {
				System.out.println("成功的事物操作: " + result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(jedis != null) {
				jedis.close();
			}
		}
	}
}
